<template>
  <div class="app-container certification-review">
    <h2>供应商认证管理</h2>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="对应用户id" prop="supplyId">
        <el-input
          v-model="queryParams.supplyId"
          placeholder="请输入对应用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司联系人" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入公司联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商电话" prop="supplyPhone">
        <el-input
          v-model="queryParams.supplyPhone"
          placeholder="请输入供应商电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请角色身份" prop="roleId">
        <el-select v-model="queryParams.roleId" placeholder="请选择申请角色身份" clearable>
          <el-option
            v-for="dict in dict.type.sup_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option label="待审核" value="0" />
          <el-option label="已通过" value="1" />
          <el-option label="未通过" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supply:authSupUser:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['supply:authSupUser:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['supply:authSupUser:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supply:authSupUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="authSupUserList" @selection-change="handleSelectionChange" @row-click="handleRowClick" ref="table">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="公司联系人" align="center" prop="supplyName" />
      <el-table-column label="供应商电话" align="center" prop="supplyPhone" />
      <el-table-column label="申请角色身份" align="center" prop="roleId" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sup_user_type" :value="scope.row.roleId"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click.stop="handleDetail(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click.stop="handlePass(scope.row)"
            v-if="scope.row.status == 0"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click.stop="showReasonInput(scope.row)"
            v-if="scope.row.status == 0"
          >不通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看认证供应商详情 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-card v-if="form" class="review-card">
        <div slot="header" class="clearfix">
          <span>供应商: {{ form.userName }}</span>
          <el-tag :type="getStatusType(form.status)">{{ getStatusText(form.status) }}</el-tag>
        </div>

        <el-descriptions title="基本信息" :column="2" border>
          <el-descriptions-item label="公司名称">{{ form.companyName }}</el-descriptions-item>
          <el-descriptions-item label="公司联系人">{{ form.supplyName }}</el-descriptions-item>
          <el-descriptions-item label="供应商电话">{{ form.supplyPhone }}</el-descriptions-item>
          <el-descriptions-item label="担保公司">{{ form.vouchCompanyName }}</el-descriptions-item>
          <el-descriptions-item label="纳税人">{{ form.taxpayer }}</el-descriptions-item>
          <el-descriptions-item label="税号">{{ form.texCard }}</el-descriptions-item>
          <el-descriptions-item label="申请角色身份">
            <dict-tag :options="dict.type.sup_user_type" :value="form.roleId"/>
          </el-descriptions-item>
          <el-descriptions-item label="公司地址">{{ form.address }}</el-descriptions-item>
          <el-descriptions-item label="商品类型">{{ form.typeList }}</el-descriptions-item>
          <el-descriptions-item label="拒绝原因" v-if="form.status===2" >{{ form.reason }}</el-descriptions-item>
        </el-descriptions>

        <el-descriptions title="证件信息" :column="1" border class="mt20">
          <el-descriptions-item label="营业执照">
            <el-image
              v-if="form.businessLicense"
              :src="form.businessLicense"
              style="width: 200px; height: 120px"
              :preview-src-list="[form.businessLicense]"
            />
            <span v-else>未上传</span>
          </el-descriptions-item>
          <el-descriptions-item label="法人身份证正面">
            <el-image
              v-if="form.identityCard1"
              :src="form.identityCard1"
              style="width: 200px; height: 120px"
              :preview-src-list="[form.identityCard1]"
            />
            <span v-else>未上传</span>
          </el-descriptions-item>
          <el-descriptions-item label="法人身份证反面">
            <el-image
              v-if="form.identityCard2"
              :src="form.identityCard2"
              style="width: 200px; height: 120px"
              :preview-src-list="[form.identityCard2]"
            />
            <span v-else>未上传</span>
          </el-descriptions-item>
          <el-descriptions-item label="法人照片">
            <el-image
              v-if="form.personImage"
              :src="form.personImage"
              style="width: 200px; height: 120px"
              :preview-src-list="[form.personImage]"
            />
            <span v-else>未上传</span>
          </el-descriptions-item>
        </el-descriptions>

        <el-descriptions title="合同信息" :column="1" border class="mt20">
          <el-descriptions-item label="通用合同">
            <el-link v-if="form.commonContract" :href="form.commonContract" type="primary" target="_blank">查看合同</el-link>
            <span v-else>未上传</span>
          </el-descriptions-item>
          <el-descriptions-item label="担保合同">
            <el-link v-if="form.vouchContract" :href="form.vouchContract" type="primary" target="_blank">查看合同</el-link>
            <span v-else>未上传</span>
          </el-descriptions-item>
          <el-descriptions-item label="战略合同">
            <el-link v-if="form.strategyContract" :href="form.strategyContract" type="primary" target="_blank">查看合同</el-link>
            <span v-else>未上传</span>
          </el-descriptions-item>
        </el-descriptions>

        <!-- 操作按钮 -->
        <div class="action-buttons" v-if="form.status == 0">
          <el-button type="success" @click="handlePass(form)">通过认证</el-button>
          <el-button type="danger" @click="showReasonInput(form)" v-if="!showReasonDialog">不通过</el-button>
          <el-button type="danger" @click="handleReject" v-if="showReasonDialog">确认不通过</el-button>
          <el-button @click="showReasonDialog = false" v-if="showReasonDialog">取消</el-button>
        </div>

        <!-- 拒绝原因输入框（内嵌在详情页中） -->
        <el-form v-if="showReasonDialog" ref="reasonForm" :model="reasonForm" :rules="reasonRules" label-width="100px" class="mt20">
          <el-form-item label="未通过原因" prop="reason">
            <el-input v-model="reasonForm.reason" type="textarea" :rows="3" placeholder="请输入未通过原因"></el-input>
          </el-form-item>
        </el-form>
      </el-card>
    </el-dialog>

    <!-- 添加或修改认证供应商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="对应用户id" prop="supplyId">
          <el-input v-model="form.supplyId" placeholder="请输入对应用户id" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="地址id" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入地址id" />
        </el-form-item>
        <el-form-item label="公司联系人" prop="supplyName">
          <el-input v-model="form.supplyName" placeholder="请输入公司联系人" />
        </el-form-item>
        <el-form-item label="供应商电话" prop="supplyPhone">
          <el-input v-model="form.supplyPhone" placeholder="请输入供应商电话" />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicense">
          <image-upload v-model="form.businessLicense"/>
        </el-form-item>
        <el-form-item label="法人身份证正面" prop="identityCard1">
          <image-upload v-model="form.identityCard1"/>
        </el-form-item>
        <el-form-item label="法人身份证反面" prop="identityCard2">
          <image-upload v-model="form.identityCard2"/>
        </el-form-item>
        <el-form-item label="通用合同" prop="commonContract">
          <file-upload v-model="form.commonContract"/>
        </el-form-item>
        <el-form-item label="法人照片" prop="personImage">
          <image-upload v-model="form.personImage"/>
        </el-form-item>
        <el-form-item label="纳税人" prop="taxpayer">
          <el-input v-model="form.taxpayer" placeholder="请输入纳税人" />
        </el-form-item>
        <el-form-item label="税号" prop="texCard">
          <el-input v-model="form.texCard" placeholder="请输入税号" />
        </el-form-item>
        <el-form-item label="申请角色身份" prop="roleId">
          <el-select v-model="form.roleId" placeholder="请选择申请角色身份">
            <el-option
              v-for="dict in dict.type.sup_user_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="担保公司" prop="vouchId">
          <el-input v-model="form.vouchId" placeholder="请输入担保公司" />
        </el-form-item>
        <el-form-item label="担保合同" prop="vouchContract">
          <file-upload v-model="form.vouchContract"/>
        </el-form-item>
        <el-form-item label="战略合同" prop="strategyContract">
          <file-upload v-model="form.strategyContract"/>
        </el-form-item>
        <el-form-item label="拒绝原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="经营商品分类集合" prop="typeList">
          <el-input v-model="form.typeList" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAuthSupUser, getAuthSupUser, delAuthSupUser, addAuthSupUser, updateAuthSupUser,passAuthSupUser,unPassAuthSupUser } from "@/api/supply/authSupUser";

export default {
  name: "AuthSupUser",
  dicts: ['sup_user_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 认证供应商表格数据
      authSupUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情弹窗显示
      detailOpen: false,
      // 拒绝原因显示
      showReasonDialog: false,
      // 当前选中记录
      selectedSupUser: null,
      // 拒绝原因表单
      reasonForm: {
        id: null,
        reason: '',
      },
      // 拒绝原因验证规则
      reasonRules: {
        reason: [
          { required: true, message: "请输入未通过原因", trigger: "blur" }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplyId: null,
        companyName: null,
        addressId: null,
        supplyName: null,
        supplyPhone: null,
        taxpayer: null,
        texCard: null,
        roleId: null,
        vouchId: null,
        vouchContract: null,
        strategyContract: null,
        status: null,
        reason: null,
        typeList: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplyId: [
          { required: true, message: "对应用户id不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        supplyName: [
          { required: true, message: "公司联系人不能为空", trigger: "blur" }
        ],
        businessLicense: [
          { required: true, message: "营业执照不能为空", trigger: "blur" }
        ],
        // identityCard1: [
        //   { required: true, message: "法人身份证正面不能为空", trigger: "blur" }
        // ],
        // identityCard2: [
        //   { required: true, message: "法人身份证反面不能为空", trigger: "blur" }
        // ],
        commonContract: [
          { required: true, message: "通用合同不能为空", trigger: "blur" }
        ],
        taxpayer: [
          { required: true, message: "纳税人不能为空", trigger: "blur" }
        ],
        texCard: [
          { required: true, message: "税号不能为空", trigger: "blur" }
        ],
        roleId: [
          { required: true, message: "申请角色身份不能为空", trigger: "change" }
        ],
        // vouchContract: [
        //   { required: true, message: "担保合同不能为空", trigger: "blur" }
        // ],
        status: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        typeList: [
          { required: true, message: "经营商品分类集合不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询认证供应商列表 */
    getList() {
      this.loading = true;
      listAuthSupUser(this.queryParams).then(response => {
        this.authSupUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName:null,
        supplyId: null,
        companyName: null,
        addressId: null,
        supplyName: null,
        supplyPhone: null,
        businessLicense: null,
        identityCard1: null,
        identityCard2: null,
        commonContract: null,
        personImage: null,
        taxpayer: null,
        texCard: null,
        roleId: null,
        vouchId: null,
        vouchContract: null,
        strategyContract: null,
        status: null,
        reason: null,
        typeList: null,
        judgerName:null,
        createTime:null,
        updateTime:null,
        address:null,
        vouchCompanyName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 点击行
    handleRowClick(row) {
      // 防止与选择框冲突
      this.$refs.table && this.$refs.table.clearSelection();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加认证供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAuthSupUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改认证供应商";
      });
    },
    /** 查看详情操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id
      getAuthSupUser(id).then(response => {
        this.form = response.data;
        this.detailOpen = true;
        this.title = "供应商详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuthSupUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuthSupUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 处理认证通过 */
    handlePass(row) {
      const id = row.id;
      this.$confirm('确认通过该供应商的认证申请吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 创建一个包含状态和ID的对象
        const data = {
          id: id,
          status: "1" // 已通过
        };
        passAuthSupUser(id).then(response => {
          this.$modal.msgSuccess("认证通过成功");
          this.detailOpen = false;
          this.getList();
        });
      }).catch(() => {
        this.$message.info('已取消操作');
      });
    },
    /** 显示拒绝原因输入框 */
    showReasonInput(row) {
      this.selectedSupUser = row;
      this.reasonForm.id = row.id;
      this.reasonForm.reason = '';
      this.showReasonDialog = true;
    },
    /** 处理认证不通过 */
    handleReject() {
      this.$refs.reasonForm.validate(valid => {
        if (valid) {
          this.$confirm('确认拒绝该供应商的认证申请吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 创建一个包含状态、拒绝原因和ID的对象
            const data = {
              id: this.reasonForm.id,
              status: "2", // 未通过
              reason: this.reasonForm.reason
            };
            updateAuthSupUser(data).then(response => {
              this.$modal.msgSuccess("已拒绝该供应商认证申请");
              this.showReasonDialog = false;
              this.detailOpen = false;
              this.getList();
            });
          }).catch(() => {
            this.$message.info('已取消操作');
          });
        } else {
          this.$message.error('请填写未通过原因');
        }
      });
    },
    /** 根据状态返回Tag类型 */
    getStatusType(status) {
      switch (status) {
        case 0:
        case '0':
          return 'warning';
        case 1:
        case '1':
          return 'success';
        case 2:
        case '2':
          return 'danger';
        default:
          return 'info';
      }
    },
    /** 根据状态返回文本 */
    getStatusText(status) {
      switch (status) {
        case 0:
        case '0':
          return '待审核';
        case 1:
        case '1':
          return '已通过';
        case 2:
        case '2':
          return '未通过';
        default:
          return '未知';
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除认证供应商编号为"' + ids + '"的数据项？').then(function() {
        return delAuthSupUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supply/authSupUser/export', {
        ...this.queryParams
      }, `authSupUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.certification-review {
  padding: 20px;
}

.review-card {
  width: 100%;
}

.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.action-buttons {
  margin-top: 20px;
  text-align: center;
}

.action-buttons .el-button {
  margin: 0 10px;
}

.mt20 {
  margin-top: 20px;
}
</style>
